package com.wufun.overseas.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.wufun.overseas.R;
import com.wufun.overseas.view.lib.ImageFolder;
import com.wufun.overseas.view.lib.ImageLoader;

import java.util.List;


/**
 * Created by xiet on 2015/7/8.
 */
public class AlbumAdapter extends BaseAdapter {

    private Context mContext;
    private List<ImageFolder> folders;

    public AlbumAdapter(Context context, List<ImageFolder> folders) {
        this.mContext = context;
        this.folders = folders;
        mImageLoader = new ImageLoader(1, ImageLoader.Type.LIFO);
    }

    @Override
    public int getCount() {
        if (folders == null) return 0;
        return folders.size();
    }

    @Override
    public Object getItem(int position) {
        return folders.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mContext == null || getCount() == 0) return convertView;

        try {

            ViewHolder holder;
            if (convertView != null) {
                holder = (ViewHolder) convertView.getTag();
            } else {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.album_item, null);

                holder.cover = (ImageView) convertView.findViewById(R.id.album_cover);
                holder.name = (TextView) convertView.findViewById(R.id.album_name);
                holder.count = (TextView) convertView.findViewById(R.id.album_count);

                convertView.setTag(holder);
            }

            ImageFolder item = folders.get(position);
            setImageByUrl(holder.cover, item.getFirstImagePath());
            holder.name.setText(item.getName());
            holder.count.setText("(" + item.getCount() + ")");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    public static ImageLoader imageLoader() {
        return ImageLoader.getInstance(3, ImageLoader.Type.LIFO);
    }

    ImageLoader mImageLoader;

    private void setImageByUrl(ImageView view, String url) {
        mImageLoader.loadImage(url, view);
//        imageLoader().loadImage(url, view);
//        BitmapDrawable drawable = new BitmapDrawable(url);
//        view.setImageDrawable(drawable);
//        drawable = null;
    }

    public class ViewHolder {
        ImageView cover;
        TextView name;
        TextView count;
    }
}